{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3.1 \u6570\u5b57\u7684\u56db\u820d\u4e94\u5165\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5bf9\u6d6e\u70b9\u6570\u6267\u884c\u6307\u5b9a\u7cbe\u5ea6\u7684\u820d\u5165\u8fd0\u7b97\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u7b80\u5355\u7684\u820d\u5165\u8fd0\u7b97\uff0c\u4f7f\u7528\u5185\u7f6e\u7684 round(value, ndigits) \u51fd\u6570\u5373\u53ef\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(1.23, 1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(1.27, 1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(-1.27, 1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(1.25361,3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4e00\u4e2a\u503c\u521a\u597d\u5728\u4e24\u4e2a\u8fb9\u754c\u7684\u4e2d\u95f4\u7684\u65f6\u5019\uff0c round \u51fd\u6570\u8fd4\u56de\u79bb\u5b83\u6700\u8fd1\u7684\u5076\u6570\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf91.5\u6216\u80052.5\u7684\u820d\u5165\u8fd0\u7b97\u90fd\u4f1a\u5f97\u52302\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f20\u7ed9 round() \u51fd\u6570\u7684 ndigits \u53c2\u6570\u53ef\u4ee5\u662f\u8d1f\u6570\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\n\u820d\u5165\u8fd0\u7b97\u4f1a\u4f5c\u7528\u5728\u5341\u4f4d\u3001\u767e\u4f4d\u3001\u5343\u4f4d\u7b49\u4e0a\u9762\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 1627731\nround(a, -1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(a, -2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "round(a, -3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8981\u5c06\u820d\u5165\u548c\u683c\u5f0f\u5316\u8f93\u51fa\u641e\u6df7\u6dc6\u4e86\u3002\n\u5982\u679c\u4f60\u7684\u76ee\u7684\u53ea\u662f\u7b80\u5355\u7684\u8f93\u51fa\u4e00\u5b9a\u5bbd\u5ea6\u7684\u6570\uff0c\u4f60\u4e0d\u9700\u8981\u4f7f\u7528 round() \u51fd\u6570\u3002\n\u800c\u4ec5\u4ec5\u53ea\u9700\u8981\u5728\u683c\u5f0f\u5316\u7684\u65f6\u5019\u6307\u5b9a\u7cbe\u5ea6\u5373\u53ef\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "x = 1.23456\nformat(x, '0.2f')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "format(x, '0.3f')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "'value is {:0.3f}'.format(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u540c\u6837\uff0c\u4e0d\u8981\u8bd5\u7740\u53bb\u820d\u5165\u6d6e\u70b9\u503c\u6765\u201d\u4fee\u6b63\u201d\u8868\u9762\u4e0a\u770b\u8d77\u6765\u6b63\u786e\u7684\u95ee\u9898\u3002\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u503e\u5411\u4e8e\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = 2.1\nb = 4.2\nc = a + b\nc"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = round(c, 2) # \"Fix\" result (???)\nc"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u591a\u6570\u4f7f\u7528\u5230\u6d6e\u70b9\u7684\u7a0b\u5e8f\uff0c\u6ca1\u6709\u5fc5\u8981\u4e5f\u4e0d\u63a8\u8350\u8fd9\u6837\u505a\u3002\n\u5c3d\u7ba1\u5728\u8ba1\u7b97\u7684\u65f6\u5019\u4f1a\u6709\u4e00\u70b9\u70b9\u5c0f\u7684\u8bef\u5dee\uff0c\u4f46\u662f\u8fd9\u4e9b\u5c0f\u7684\u8bef\u5dee\u662f\u80fd\u88ab\u7406\u89e3\u4e0e\u5bb9\u5fcd\u7684\u3002\n\u5982\u679c\u4e0d\u80fd\u5141\u8bb8\u8fd9\u6837\u7684\u5c0f\u8bef\u5dee(\u6bd4\u5982\u6d89\u53ca\u5230\u91d1\u878d\u9886\u57df)\uff0c\u90a3\u4e48\u5c31\u5f97\u8003\u8651\u4f7f\u7528 decimal \u6a21\u5757\u4e86\uff0c\u4e0b\u4e00\u8282\u6211\u4eec\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}